package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import net.sf.json.JSONObject;
/**
安正接口集成
*/
public class IbqInterface implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(IbqInterface.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			PropertiesUtil properties = new PropertiesUtil();
			def ibqurl = properties.readValue("ibqurl"); //获取webservice接口地址
			def appid = properties.readValue("appid"); //获取appId
			def body = Util.getRequestBody(request,'utf-8'); //获取报文
			JSONObject obj = JSONObject.fromObject(body); //解析报文
			def method = obj.get("method"); //获取接口名称
			def content = obj.get("content");
			JSONObject inparam = JSONObject.fromObject(content);
			Iterator<String> it = inparam.keys();
			def params = "";			
			def json_key = "";
			def json_value = "";			
			while(it.hasNext()){
				json_key = it.next();
				json_value = inparam.get(json_key);
				if(json_value!=""){
					params=params.concat("<params><name>"+json_key+"</name><operator>equal</operator><value>"+json_value+"</value></params>")
				}
			};
			def xmlBody = """
							<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
								<SOAP-ENV:Body>
									<m:query xmlns:m="http://service.web.server.ua.businessmatrix.com/">
										<appId>"""+appid+"""</appId>
										<name>"""+method+"""</name>
										"""+params+"""
									</m:query>
								</SOAP-ENV:Body>
							</SOAP-ENV:Envelope>""";
							
			log.debug("请求报文： "+xmlBody);			
			def charset = "utf-8"; //设置字符集
			def timeout = 100000; //设置请求超时时间
			return Util.callWebService(ibqurl,xmlBody,charset,"",timeout);
		}catch(Exception e) {
			log.error("错误代码： ",e);
			return "{\"msg\": \"请求接口失败！\",\"code\": -1}"
		}
	}	
}